// owner-app/pages/form-my-dwell/index.js
import { handlerGobackClick, hint, formatters } from "../../../utils/util";
const app = getApp();
import Apipublic from "../../../models/api";
const apipublic = new Apipublic();
import CompleteInfor from "../../../models/completeInformation";
const completeInfor = new CompleteInfor();
import User from "../../../models/user";
const user = new User();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    baseImgUrl: "",
    options: {},
    minDate: new Date(2000, 0, 1).getTime(),
    maxDate: new Date(2099, 0, 1).getTime(),
    //居住信息表单
    commName: "",
    buildName: "",
    unitName: "",
    floorName: "",
    houseName: "",
    kinshipDesc: "",
    startDate: "",
    endDate: "",
    startDateDesc: "",
    endDateDesc: "",
    houseCategoryDesc: "",
    houseNatureDesc: "",
    houseUseDesc: "",
    houseStatusDesc: "",
    houseStructDesc: "",
    houseCount: "",
    area: "",
    isRent: "",
    rentCount: "",
    rentArea: "",
    rentUseDesc: "",
    houseTypeDesc: "",
    houseCard: "",
    //居住信息表单对应弹框
    commNameShow: false,
    buildNameShow: false,
    unitNameShow: false,
    floorNameShow: false,
    houseNameShow: false,
    kinshipDescShow: false,
    startDateShow: false,
    endDateShow: false,
    houseCategoryDescShow: false,
    houseNatureDescShow: false,
    houseUseDescShow: false,
    houseStatusDescShow: false,
    houseStructDescShow: false,
    isRentShow: false,
    rentUseDescShow: false,
    houseTypeDescShow: false,
    columnsCommName: [],
    columnsBuildName: [],
    columnsUnitName: [],
    columnsFloorName: [],
    columnsHouseName: [],
    columnsKinshipDesc: [],
    columnsHouseCategoryDesc: [],
    columnHouseNatureDesc: [],
    columnHouseUseDesc: [],
    columnHouseStatusDesc: [],
    columnHouseStructDesc: [],
    columnIsRent: [],
    columnRentUseDesc: [],
    columnHouseTypeDesc: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      options,
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var { baseImgUrl } = getApp().globalImgUrl.config;
    this.setData({
      baseImgUrl,
    });

    this.classify();
  },
  handlerGobackClick,
  onChange(e) {
    let key = e.currentTarget.dataset.key;
    let value = e.detail;
    this.setData({
      [key]: value,
    });
  },
  roomBlur(e) {
    let key = e.currentTarget.dataset.key;
    // 出租间数必须小于等于房屋间数;
    //房屋间数
    if (key == "houseCount") {
      if (!this.data.rentCount) return;
      if (Number(this.data.houseCount) < Number(this.data.rentCount)) {
        hint("房屋间数不能小于出租间数");
        this.setData({
          houseCount: "",
        });
      }
    } else if (key == "rentCount") {
      //出租间数
      if (!this.data.houseCount) return;

      if (Number(this.data.houseCount) < Number(this.data.rentCount)) {
        hint("出租间数不能大于房屋间数");
        this.setData({
          rentCount: "",
        });
      }
    }

    //出租面积必须小于等于房屋面积
    //房屋面积
    if (key == "area") {
      if (!this.data.rentArea) return;
      if (Number(this.data.area) < Number(this.data.rentArea)) {
        hint("房屋面积不能小于出租面积");
        this.setData({
          area: "",
        });
      }
    } else if (key == "rentArea") {
      //出租面积
      if (!this.data.area) return;
      if (Number(this.data.area) < Number(this.data.rentArea)) {
        hint("出租面积不能大于房屋面积");
        this.setData({
          rentArea: "",
        });
      }
    }
  },
  // 选择小区--搜索
  onBlurCommname(e) {
    let columnsCommName = [];
    let value = e.detail.value;

    if (!e.detail.value) {
      this.setData({
        commId: "",
        commName: "",
      });
      return false;
    }

    completeInfor.userInfoAddress({ commName: value }).then((res) => {
      if (res.result.length) {
        columnsCommName = res.result.map((v) => ({
          text: v.commName,
          value: v.commId,
        }));

        this.setData({
          commNameShow: true,
          columnsCommName,
        });
      } else {
        hint("未查询到相关数据");
        this.setData({
          commId: this.data.commId || "",
          commName: this.data.commName || "",
        });
      }
    });
  },
  // 控制选择框显示隐藏--房屋信息的
  handleShowPickerDwell(e) {
    var showflag = e.currentTarget.dataset.showflag;
    var { showname } = e.currentTarget.dataset;
    if (!showflag) {
      this.setData({
        [showname]: false,
      });
      return false;
    }

    // 选择楼号
    if ([showname] == "buildNameShow" && !this.data.commId) {
      hint("请先选择小区");
      return false;
    }
    if ([showname] == "buildNameShow" && this.data.commId) {
      if (!JSON.parse(showflag)) {
        this.setData({
          commId: this.data.commId || "",
          commName: this.data.commName || "",
          [showname]: JSON.parse(e.currentTarget.dataset.showflag),
        });
        return;
      }

      let columnsBuildName = [];
      completeInfor
        .userInfoAddress({ commId: this.data.commId })
        .then((res) => {
          columnsBuildName = res.result.map((v) => ({
            text: v.buildName,
            value: v.buildId,
          }));

          this.setData({
            columnsBuildName,
          });

          if (
            !this.data.columnsBuildName.length ||
            !this.data.columnsBuildName[0].text
          ) {
            hint("暂无数据");
          }
        });

      if (
        !this.data.columnsBuildName.length ||
        !this.data.columnsBuildName[0].text
      ) {
        return false;
      }
    }

    // 选择单元
    if ([showname] == "unitNameShow" && !this.data.buildId) {
      hint("请先选择楼号");
      return false;
    }
    if ([showname] == "unitNameShow" && this.data.buildId) {
      if (!JSON.parse(showflag)) {
        this.setData({
          buildId: this.data.buildId || "",
          buildName: this.data.buildName || "",
          [showname]: JSON.parse(e.currentTarget.dataset.showflag),
        });
        return;
      }

      let columnsUnitName = [];
      completeInfor
        .userInfoAddress({ buildId: this.data.buildId })
        .then((res) => {
          columnsUnitName = res.result.map((v) => ({
            text: v.unitName,
            value: v.unitId,
          }));
          this.setData({
            columnsUnitName,
          });
        });
    }

    // 选择楼层
    if ([showname] == "floorNameShow" && !this.data.unitId) {
      hint("请先选择单元");
      return false;
    }
    if ([showname] == "floorNameShow" && this.data.unitId) {
      if (!JSON.parse(showflag)) {
        this.setData({
          unitId: this.data.unitId || "",
          unitIdName: this.data.unitIdName || "",
          [showname]: JSON.parse(e.currentTarget.dataset.showflag),
        });
        return;
      }

      let columnsFloorName = [];
      completeInfor
        .userInfoAddress({ unitId: this.data.unitId })
        .then((res) => {
          columnsFloorName = res.result.map((v) => ({
            text: v.floorName,
            value: v.floorId,
          }));
          this.setData({
            columnsFloorName,
          });
        });
    }

    // 选择房间号
    if ([showname] == "houseNameShow" && !this.data.floorId) {
      hint("请先选择楼层");
      return false;
    }
    if ([showname] == "houseNameShow" && this.data.floorId) {
      if (!JSON.parse(showflag)) {
        this.setData({
          floorId: this.data.floorId || "",
          floorIdName: this.data.floorIdName || "",
          [showname]: JSON.parse(e.currentTarget.dataset.showflag),
        });
        return;
      }

      let columnsHouseName = [];
      completeInfor
        .userInfoAddress({ floorId: this.data.floorId })
        .then((res) => {
          columnsHouseName = res.result.map((v) => ({
            text: v.houseName,
            value: v.houseId,
          }));

          this.setData({
            columnsHouseName,
          });
        });
    }

    // 选择与户主关系
    if ([showname] == "kinshipDescShow" && !this.data.houseId) {
      hint("请先选择房间号");
      return false;
    }

    this.setData({
      [showname]: JSON.parse(e.currentTarget.dataset.showflag),
    });
  },
  handleShowPickerDwellTime(e) {
    var showflag = e.currentTarget.dataset.showflag;
    var { showname } = e.currentTarget.dataset;
    if (!showflag) {
      this.setData({
        [showname]: false,
      });
      return false;
    }

    this.setData({
      minDate: new Date(2000, 0, 1).getTime(),
      maxDate: new Date(2099, 0, 1).getTime(),
    });

    // 开始时间
    if (showname == "startDateShow" && this.data.endDate) {
      this.setData({
        maxDate: this.data.endDateDesc,
      });
    }

    // 结束时间
    if (showname == "endDateShow" && this.data.startDate) {
      this.setData({
        minDate: this.data.startDateDesc,
      });
    }

    this.setData({
      [showname]: JSON.parse(e.currentTarget.dataset.showflag),
    });
  },
  // 保存
  submitLastAll() {
    if (!this.data.commId) {
      hint("请选择小区");
      return false;
    }

    if (!this.data.buildId) {
      hint("请选择楼号");
      return false;
    }

    if (!this.data.unitId) {
      hint("请选择单元");
      return false;
    }

    if (!this.data.floorId) {
      hint("请选择楼层");
      return false;
    }

    if (!this.data.houseId) {
      hint("请选择房间号");
      return false;
    }

    if (!this.data.kinship) {
      hint("请选择与户主关系");
      return false;
    }

    // 与户主关系：房主本人
    if (this.data.kinship == 0) {
      if (!this.data.houseCategory) {
        hint("请选择房屋类别");
        return false;
      }

      if (!this.data.houseNature) {
        hint("请选择房屋性质");
        return false;
      }

      if (!this.data.houseUse) {
        hint("请选择房屋用途");
        return false;
      }

      if (!this.data.houseStatus) {
        hint("请选择房屋现状");
        return false;
      }

      if (!this.data.houseStruct) {
        hint("请选择房屋结构");
        return false;
      }

      if (!this.data.houseCount) {
        hint("请输入房屋间数");
        return false;
      }

      if (!this.data.area) {
        hint("请输入房屋面积");
        return false;
      }

      if (!this.data.isRent) {
        hint("请选择是否出租房");
        return false;
      }

      if (this.data.isRent != "否") {
        if (!this.data.rentCount) {
          hint("请选择出租间数");
          return false;
        }

        if (!this.data.rentArea) {
          hint("请输入出租面积");
          return false;
        }
      }
    }

    // 身份为：租户时显示
    if (this.data.options.userType == "Household") {
      if (!this.data.startDate) {
        hint("请选择租住开始时间");
        return false;
      }

      if (!this.data.endDate) {
        hint("请选择租住开始时间");
        return false;
      }
    }

    let data = {
      commId: this.data.commId,
      commName: this.data.commName,
      buildId: this.data.buildId,
      unitId: this.data.unitId,
      floorId: this.data.floorId,
      houseId: this.data.houseId,
      poe: this.data.poe,
      entryTime: this.data.entryTime,
      validityTerm: this.data.validityTerm,
      kinship: this.data.kinship,
      houseCategory: this.data.houseCategory,
      houseNature: this.data.houseNature,
      houseUse: this.data.houseUse,
      houseCount: this.data.houseCount,
      area: this.data.area,
      isRent: this.data.isRent,
      rentCount: this.data.rentCount,
      rentArea: this.data.rentArea,
      facePath: this.data.facePath,
      phoneNum: wx.getStorageSync("user").phone,
      houseStatus: this.data.houseStatus,
      houseStruct: this.data.houseStruct,
      rentUse: this.data.rentUse,
      houseType: this.data.houseType,
      houseCard: this.data.houseCard,
      startDate: this.data.startDate,
      endDate: this.data.endDate,
      userType: this.data.options.userType,
    };

    user.houseAddress(data).then((res) => {
      if (res.code == 1000) {
        hint("添加住址成功");
        wx.reLaunch({
          url: "/owner-app/pages/my-dwell/index",
        });
      }
    });
  },
  commNameConfirm(e) {
    this.setData({
      commId: e.detail.value.value,
      commName: e.detail.value.text,
      commNameShow: false,
      buildId: "",
      buildName: "",
      unitId: "",
      unitName: "",
      floorId: "",
      floorName: "",
      houseId: "",
      houseName: "",
    });
  },
  buildNameConfirm(e) {
    this.setData({
      buildId: e.detail.value.value,
      buildName: e.detail.value.text,
      buildNameShow: false,
      unitId: "",
      unitName: "",
      floorId: "",
      floorName: "",
      houseId: "",
      houseName: "",
    });
  },
  unitNameConfirm(e) {
    this.setData({
      unitId: e.detail.value.value,
      unitName: e.detail.value.text,
      unitNameShow: false,
      floorId: "",
      floorName: "",
      houseId: "",
      houseName: "",
    });
  },
  floorNameConfirm(e) {
    this.setData({
      floorId: e.detail.value.value,
      floorName: e.detail.value.text,
      floorNameShow: false,
      houseId: "",
      houseName: "",
    });
  },
  houseNameConfirm(e) {
    this.setData({
      houseId: e.detail.value.value,
      houseName: e.detail.value.text,
      houseNameShow: false,
    });
  },
  kinshipDescConfirm(e) {
    this.setData({
      kinship: e.detail.value.value,
      kinshipDesc: e.detail.value.text,
      kinshipDescShow: false,
    });

    apipublic
      .userInfoHouseHold({
        memberId: wx.getStorageSync("user").memberId,
        houseId: this.data.houseId,
      })
      .then((res) => {
        if (res.code == 1001 && e.detail.value.value == "0") {
          hint(res.message);
          this.setData({
            kinship: null,
            kinshipDesc: null,
            houseCategory: null,
            houseCategoryDesc: null,
            houseNature: null,
            houseNatureDesc: null,
            houseUse: null,
            houseUseDesc: null,
            houseStatus: null,
            houseStatusDesc: null,
            houseStruct: null,
            houseStructDesc: null,
            houseCount: null,
            area: null,
            mianjiDesc: null,
          });
        }
      });

    // 与户主关系: 不是房主本人
    if (e.detail.value.value != 0) {
      this.setData({
        houseCategory: null,
        houseCategoryDesc: null,
        houseNature: null,
        houseNatureDesc: null,
        houseUse: null,
        houseUseDesc: null,
        houseStatus: null,
        houseStatusDesc: null,
        houseStruct: null,
        houseStructDesc: null,
        houseCount: null,
        area: null,
        mianjiDesc: null,
      });
    }
  },
  selectedTime(e) {
    var { value, showname } = e.currentTarget.dataset;
    this.setData({
      [value]: formatters(e.detail, "YY-MM-DD"),
      [value + "Desc"]: e.detail,
      [showname]: false,
    });
  },
  houseCategoryDescConfirm(e) {
    this.setData({
      houseCategory: e.detail.value.value,
      houseCategoryDesc: e.detail.value.text,
      houseCategoryDescShow: false,
    });
  },
  houseNatureDescConfirm(e) {
    this.setData({
      houseNature: e.detail.value.value,
      houseNatureDesc: e.detail.value.text,
      houseNatureDescShow: false,
    });
  },
  houseUseDescConfirm(e) {
    this.setData({
      houseUse: e.detail.value.value,
      houseUseDesc: e.detail.value.text,
      houseUseDescShow: false,
    });
  },
  houseStatusDescConfirm(e) {
    this.setData({
      houseStatus: e.detail.value.value,
      houseStatusDesc: e.detail.value.text,
      houseStatusDescShow: false,
    });
  },
  houseStructDescConfirm(e) {
    this.setData({
      houseStruct: e.detail.value.value,
      houseStructDesc: e.detail.value.text,
      houseStructDescShow: false,
    });
  },
  isRentConfirm(e) {
    // 这个地方value和title一样
    this.setData({
      isRent: e.detail.value.value,
      isRentShow: false,
    });

    // 是否出租房: 否
    if (e.detail.value.value != "是") {
      this.setData({
        rentCount: null,
        rentArea: null,
        rentUse: null,
        rentUseDesc: null,
      });
    }
  },
  rentUseDescConfirm(e) {
    this.setData({
      rentUse: e.detail.value.value,
      rentUseDesc: e.detail.value.text,
      rentUseDescShow: false,
    });
  },
  houseTypeDescConfirm(e) {
    this.setData({
      houseType: e.detail.value.value,
      houseTypeDesc: e.detail.value.text,
      houseTypeDescShow: false,
    });
  },
  classify() {
    //证件类型
    apipublic.dictItemList("types_of_certificate").then((res) => {
      this.setData({
        columnTypesOfCertificate: res.result,
      });
    });

    // 性别
    apipublic.dictItemList("sex").then((res) => {
      this.setData({
        columnsSex: res.result,
      });
    });

    // 民族
    apipublic.dictItemList("nation").then((res) => {
      this.setData({
        columnsNation: res.result,
      });
    });

    // 国籍
    apipublic.dictItemList("nationality").then((res) => {
      this.setData({
        columnsCountryDesc: res.result,
      });
    });

    // 与户主关系
    apipublic.dictItemList("kinship	").then((res) => {
      this.setData({
        columnsKinshipDesc: res.result,
      });

      if (this.data.options.userType == "Household") {
        var result = res.result.filter((item) => {
          return item.value != 0;
        });

        this.setData({
          columnsKinshipDesc: result,
        });
      }
    });

    // 房屋类别
    apipublic.dictItemList("house_category").then((res) => {
      this.setData({
        columnsHouseCategoryDesc: res.result,
      });
    });

    // 房屋性质
    apipublic.dictItemList("house_nature").then((res) => {
      this.setData({
        columnHouseNatureDesc: res.result,
      });
    });

    // 房屋用途
    apipublic.dictItemList("house_use").then((res) => {
      this.setData({
        columnHouseUseDesc: res.result,
      });
    });

    // 房屋现状
    apipublic.dictItemList("house_status").then((res) => {
      this.setData({
        columnHouseStatusDesc: res.result,
      });
    });

    // 房屋结构
    apipublic.dictItemList("house_struct").then((res) => {
      this.setData({
        columnHouseStructDesc: res.result,
      });
    });

    // 是否出租房
    apipublic.dictItemList("isRent").then((res) => {
      this.setData({
        columnIsRent: res.result,
      });
    });

    // 出租用途
    apipublic.dictItemList("rent_use").then((res) => {
      this.setData({
        columnRentUseDesc: res.result,
      });
    });

    // 房屋户型
    apipublic.dictItemList("house_type").then((res) => {
      this.setData({
        columnHouseTypeDesc: res.result,
      });
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
